﻿using Apps.Common;
using Apps.Models;
using Apps.Models.S;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.BLL.S
{
    public partial class S_SAMPLE_CHECKBLL
    {
        //public DBContainer db = new DBContainer();

        public List<S_BLENDSAMPLEModel> GetList(ref GridPager pager, string batch, string startdate1, string startdate2,
                string enddate1, string enddate2)
        {
            IQueryable<S_BLENDSAMPLE> queryData = this.db.S_BLENDSAMPLE.AsQueryable<S_BLENDSAMPLE>();
            if (!string.IsNullOrEmpty(batch))
            {
                queryData = queryData.Where(m => m.BATCH_CODE == batch);
            }
            if (!string.IsNullOrEmpty(startdate1))
            {
                DateTime dStartDate1 = DateTime.ParseExact(startdate1, "yyyy-MM-dd", null);
                queryData = queryData.Where(m => m.BEGIN_TIME >= dStartDate1);
            }
            if (!string.IsNullOrEmpty(startdate2))
            {
                DateTime dStartDate2 = DateTime.ParseExact(startdate2, "yyyy-MM-dd", null);
                queryData = queryData.Where(m => m.BEGIN_TIME <= dStartDate2);
            }
            if (!string.IsNullOrEmpty(enddate1))
            {
                DateTime dEndDate1 = DateTime.ParseExact(enddate1, "yyyy-MM-dd", null);
                queryData = queryData.Where(m => m.END_TIME >= dEndDate1);
            }
            if (!string.IsNullOrEmpty(enddate2))
            {
                DateTime dEndDate2 = DateTime.ParseExact(enddate2, "yyyy-MM-dd", null);
                queryData = queryData.Where(m => m.END_TIME <= dEndDate2);
            }

            var data = from m1 in queryData
                       join m2 in db.PL_RC_DETAILS on m1.BATCH_CODE equals m2.BATCH_CODE
                       select new S_BLENDSAMPLEModel()
                       {
                           BATCH_CODE = m1.BATCH_CODE,
                           MATERIEL_TYPE_NAME = m2.PUB_MATERIEL.PUB_MATERIEL_TYPE.MATERIEL_TYPE_NAME,
                           SUPPLIER_NAME = m2.PUB_SUPPLIER.SUPPLIER_NAME,
                           DETAIL_COUNT = m1.S_BLENDSAMPLE_DETAIL.Count(),
                           RFID = m1.RFID,
                           BEGIN_TIME = m1.BEGIN_TIME,
                           END_TIME = m1.END_TIME,
                           STATUS = m1.STATUS
                       };

            pager.totalRows = data.Count();
            
            //排序
            data = LinqHelper.SortingAndPaging(data, pager.sort, pager.order, pager.page, pager.rows);
            return data.ToList();
        }
    }
}
